const BaseIpcHandler = require('../BaseIpcHandler');

class GetModelArchHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> GetModelArchHandler params = ', params);
        const { local_model_id, config_name } = params;

        // 业务逻辑
        let result = null;
        try {
            // 获取模型信息
            let model_locals = await this.app.db.models_local.GetLocalModels(local_model_id);
            let model_local = model_locals[0];
            // console.log(`model_local = `, model_local);

            let model_clouds = await this.app.models_cloud_db.models_cloud.GetCloudModels(model_local.model_id);
            let model_cloud = model_clouds[0];

            // 获取模型对应的jsproxy路径
            let jsproxy_path = model_cloud.script_jsproxy;
            const handlerClass = require(jsproxy_path);
            const jsproxy = new handlerClass();
            let data = await jsproxy.loadModelArch(config_name);
            result = { code: 0, msg: data };
        } catch (err) {
            console.error(`Error get model arch, error = : `, err);
            result = { code: -1, msg: err.message };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = GetModelArchHandler;
